package org.jypj.dev.controller;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.jypj.dev.dao.TrainNumInfoDao;
import org.jypj.dev.model.GiveWork;
import org.jypj.dev.model.TrainNumInfo;
import org.jypj.dev.service.TrainNumInfoService;
import org.jypj.dev.util.ImportExcel;
import org.jypj.dev.util.StringUtil;
import org.jypj.dev.util.work.GiveWorkUtil;
import org.jypj.dev.vo.ImportVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("trainNumImport")
public class TrainNumImportController {


    @Resource
    private TrainNumInfoService trainNumInfoService;

    /**
     * xx导入
     *
     * @param file
     * @return
     */
    @RequestMapping("importTrainNumInfos.action")
    @ResponseBody
    public Map<String, Object> importTrainNumInfos(MultipartFile file) {
        Map<String, Object> dataMap = new HashMap<>();
        int code = 0;
        String msg = "";
        try {
            List<TrainNumInfo> trainNumInfos = readTrainNumInfoExcel(file.getInputStream());
            trainNumInfoService.saveOrUpdateTrainNumInfos(trainNumInfos);
        } catch (Exception e) {
            e.printStackTrace();
        }
        dataMap.put("code", code);
        dataMap.put("msg", msg);
        return dataMap;
    }

    /**
     * 从excel中读取车组号信息
     *
     * @param in
     * @return
     */
    private List<TrainNumInfo> readTrainNumInfoExcel(InputStream in) throws IOException, InvalidFormatException {
        List<TrainNumInfo> trainNumInfos = new ArrayList<>();
        Workbook workBook = WorkbookFactory.create(in);
        Sheet sheet = workBook.getSheetAt(0);
        for (int i = 3; i < sheet.getPhysicalNumberOfRows(); i++) {
            TrainNumInfo trainNumInfo = new TrainNumInfo();
            String createTime = ImportExcel.getCellValueByIndex2(sheet, i, 1);
            String trainType = ImportExcel.getCellValueByIndex2(sheet, i, 2);
            String trainNum = ImportExcel.getCellValueByIndex2(sheet, i, 3);
            String dqzxgls = ImportExcel.getCellValueByIndex2(sheet, i, 4);
            String scxxzxgls = ImportExcel.getCellValueByIndex2(sheet, i, 5);
            String scxxhzxgls = ImportExcel.getCellValueByIndex2(sheet, i, 6);
            String scejxzxgls = ImportExcel.getCellValueByIndex2(sheet, i, 7);
            String qtejxxmfb = ImportExcel.getCellValueByIndex2(sheet, i, 8);
            if (StringUtil.isEmpty(trainNum)) {
                continue;
            }
            trainNumInfo.setId(trainNum);
            trainNumInfo.setCreateTime(createTime);
            trainNumInfo.setTrainType(trainType);
            trainNumInfo.setDqzxgl(dqzxgls);
            trainNumInfo.setScxxzxgls(scxxzxgls);
            trainNumInfo.setScxxhzxgls(scxxhzxgls);
            trainNumInfo.setScejxzxgls(scejxzxgls);
            trainNumInfo.setQtejxxmfb(qtejxxmfb);
            trainNumInfos.add(trainNumInfo);
        }
        return trainNumInfos;
    }
}
